﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ivi.Visa.Interop;
using SensitivityTest.model;
using System.IO;
using SensitivityTest.ValidProductTypeID;
namespace SensitivityTest
{
    public partial class testCommand : Form
    {
        private Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoapClient validScodeAndUpload;
        List<TestItem_RecordInfo> listNFCheckiteminfo = new List<TestItem_RecordInfo>();
        public testCommand()
        {
            InitializeComponent();
        }
        private FormattedIO488 instrumentSpec = new FormattedIO488();//488接口
        private ResourceManager rm = new ResourceManager();
        FSL fsl = new FSL();
        private void button1_Click(object sender, EventArgs e)
        {
            string ErrStr;
            try
            {
                
                instrumentSpec.IO = (IMessage)rm.Open("sa");
                instrumentSpec.IO.Timeout = 10000;
                instrumentSpec.WriteString("*rst");

                ErrStr = "频谱仪连接成功";
                MessageBox.Show(ErrStr);
            }
            catch (Exception eeee)
            {
                ErrStr = "频谱仪连接失败" + eeee.Source + eeee.Message;
                MessageBox.Show(ErrStr);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            instrumentSpec.WriteString("INITiate:CONTinuous On");
            instrumentSpec.WriteString("FREQ:CENT 1910.5 MHz");            //中心频点
            instrumentSpec.WriteString("FREQ:SPAN 5 MHz");            //span
            instrumentSpec.WriteString(":BWID:RES 1000 Hz");            //RBW
            instrumentSpec.WriteString(":BAND:VID 1000 Hz");            //VBW
            instrumentSpec.WriteString("DISP:WIND:TRAC:Y:RLEV 5 dBm");            //REF
            instrumentSpec.WriteString("INP:ATT 30 dB");            //Atten
            instrumentSpec.WriteString("DISP:WIND:TRAC:Y:RLEV:OFFS 0.5 dB");            //线损补偿：Ampudut--more1of3-- Ref Lvl Offset 
            instrumentSpec.WriteString("CALC:MARK1:MAX");
            instrumentSpec.WriteString("INITiate:CONTinuous Off");
            //instrumentSpec.WriteString("*TRG");
            instrumentSpec.WriteString("INIT:IMM");
            //instrumentSpec.WriteString("INIT:CONM");
            GetOpc();
           // System.Threading.Thread.Sleep(6000);
            string resultValue;
            instrumentSpec.WriteString("CALC:MARK1:Y?");
            resultValue = instrumentSpec.ReadString();
            MessageBox.Show(resultValue);
        }

         public void GetOpc()
        {
            instrumentSpec.WriteString("*OPC?");
            instrumentSpec.ReadString();
           
        }

         private void button3_Click(object sender, EventArgs e)
         {
             saveData();
         }

         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
         {
             if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
             {
                 e.Handled = true;
                 base.OnKeyPress(e); 
                 textBox1.Text="";
             }
         }

         public bool saveData()
         {
             Test_RecordInfo RecordDataInfo = new Test_RecordInfo();
             RecordDataInfo.ID = 1;
             RecordDataInfo.ProductSN = "SFL0200062";
             RecordDataInfo.RecordTime = DateTime.Now;
             RecordDataInfo.TestPeriod = int.Parse("100");
             RecordDataInfo.ProductTypeID = "testS001";
             RecordDataInfo.UserID = "123456";
             RecordDataInfo.TestEquipID = "10.53.28.174";
             RecordDataInfo.TestProjID = "1.0";
             RecordDataInfo.Remark = "";
             RecordDataInfo.Result = false;
            
             RecordDataInfo.TestType = "OIP3测试";

            
            TestItem_RecordInfo RecordItemDataInfo = new TestItem_RecordInfo();
            RecordItemDataInfo.ID = 1;
            RecordItemDataInfo.ProductSN = "SFL0200062";
            RecordItemDataInfo.RecordTime = DateTime.Now;
            RecordItemDataInfo.TestItemName = "RX1" + "通道 OIP3 Test";
            RecordItemDataInfo.TestItemDesc = "RX1" + "," + 1950 + "," + 1951;
            RecordItemDataInfo.Spec = "30.5";
            RecordItemDataInfo.ResValue = "-30";
                 
            RecordItemDataInfo.Result = false;
                 
            listNFCheckiteminfo.Add(RecordItemDataInfo);
                 listNFCheckiteminfo.Add(RecordItemDataInfo);


                 validScodeAndUpload = new Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoapClient("Test_Record_OIP3_DataUploadAndProcessControl_ServiceSoap", "http://10.53.20.170/pa/WebService/ServiceForClient/Test_Record_OIP3_DataUploadAndProcessControl_Service.asmx");     //上传数据
                
               
                 if (validScodeAndUpload.OIP3Data_Upload(RecordDataInfo, listNFCheckiteminfo.ToArray()))
                 {
                     MessageBox.Show("pass");
                 }
                 else
                 {
                     MessageBox.Show("fail");
                     return false;
                 }
                 return true;
         }
    }
}
